/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// JavaBase.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "ssi_scriptapi.h"
#include "datajavawindow.h"

SSI_INT64 CDataJavaWindow::sType = 0;

void StandardFunctionProcessor(SSI_FunctionAPI& theAPI);

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void DelDataNodeTree(DataNode winNode)
{
	if(winNode)
	{
		if(winNode->WinName) delete winNode->WinName;
		if(winNode->WinTag) delete winNode->WinTag;
		if(winNode->WinClass) delete winNode->WinClass;
		if(winNode->WinType) delete winNode->WinType;
		if(winNode->Next)
		{
			DelDataNodeTree(winNode->Next);
		}
		if(winNode->FChild)
		{
			DelDataNodeTree(winNode->FChild);
		}
		delete winNode;
	}
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
DataNode GetFreshNode(DataNode Ret)
{
	if(Ret)
	{
		DelDataNodeTree(Ret);
	}
	Ret = new WinDataT;
	Ret->WinName = new String;
	Ret->WinType = new String;
	Ret->WinTag = new String;
	Ret->WinClass = new String;
	
	Ret->IsParent = 0;
	Ret->IsChild = 0;
	Ret->iWinTag = 0;
	Ret->Parent = SSI_NULL;
	Ret->FChild = SSI_NULL;
	Ret->Prev = SSI_NULL;
	Ret->Next = SSI_NULL;
	return Ret;	
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
DataNode CopyPath(DataNode Original)
{
	DataNode Ret = SSI_NULL;
	if(!Original) return SSI_NULL;
	Ret = GetFreshNode(Ret);
	*Ret->WinName = *Original->WinName;
	*Ret->WinType = *Original->WinType;
	*Ret->WinTag = *Original->WinTag;
	*Ret->WinClass = *Original->WinClass;
	Ret->iWinTag = Original->iWinTag;
	Ret->IsChild = Original->IsChild;
	Ret->IsParent = Original->IsParent;
	Ret->Parent = Original->Parent;
	if(Original->FChild){
		Ret->FChild = CopyPath(Original->FChild);
	}
	return Ret;
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void RegisterFunctions(CEngine* anEngine){
	CDataJavaWindow jWind;

	CFunctionDecl aNewFunc;
	anEngine->SetPackageName("Java");

	
	/////////////////////////// JAVA ////////////////////////////////////////////////////
	aNewFunc.Clear();
	aNewFunc.Name = "JavaWin";
	aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.ReturnType = CDataJavaWindow::GetRegisteredType();
	aNewFunc.SetProcessor((ProcessingFunctionPtr)StandardFunctionProcessor);
	anEngine->AddFunction(aNewFunc);

	aNewFunc.Clear();
	aNewFunc.Name = "JavaWin";
	aNewFunc.addType(CDataInt::GetRegisteredType());
	aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.ReturnType = CDataJavaWindow::GetRegisteredType();
	aNewFunc.SetProcessor((ProcessingFunctionPtr)StandardFunctionProcessor);
	anEngine->AddFunction(aNewFunc);
}

void StandardFunctionProcessor(SSI_FunctionAPI& theAPI){
	if(theAPI.m_FunctionName == "JavaWin"){
		String sTag;
		SSI_INT64 iTag = 0;
		String sClass;

		if(theAPI.GetParameterType(0) == CDataInt::GetRegisteredType()){
			iTag = SSI_EXTRACT(CDataInt, 0);
		}else{
			sTag = SSI_EXTRACT(CDataString, 0);
		}

		sClass = SSI_EXTRACT(CDataString, 1);

		CDataJavaWindow* aWin = (CDataJavaWindow*)Factory_MakeData(CDataJavaWindow::GetRegisteredType());
		if(!aWin){
			theAPI.m_ReturnStatus = STAT_ERR;  // Internal Error => Keep
		}
		aWin->WinClass = sClass;
		aWin->WinName = "";
		aWin->WinTag = sTag;
		aWin->WinType = "jwindow";
		aWin->iWinTag = iTag;

		theAPI.SetReturnData(aWin);
		delete aWin;
		theAPI.m_ReturnStatus = STAT_OK;
		return;
	}
}

void CleanUp(CEngine* anEngine){
	CDataJavaWindow aWind;
	Factory_RemoveData(aWind.Type);
}
